package com.atguigu.gmall;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;

/**
 * @author: atguigu
 * @create: 2023-01-03 15:36
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1.创建CompletableFuture异步任务对象
        //1.1 不支持获取线程计算结果
        //CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(new Runnable() {
        //    @Override
        //    public void run() {
        //        System.out.println(Thread.currentThread().getName() + ",任务执行了");
        //    }
        //});
        //CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
        //    System.out.println(Thread.currentThread().getName() + ",任务执行了");
        //});
        //completableFuture.join();


        //1.1 支持获取线程计算结果
        //CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() {
        //    @Override
        //    public String get() {
        //        System.out.println(Thread.currentThread().getName() + ",任务执行了");
        //        return "atguigu";
        //    }
        //});
        CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
            //int i = 1/0;
            return "atguigu";
        });
        String result = future.get();
        System.out.println(result);
    }
}
